define(function(require, exports, module) {
    // 通过 require 引入依赖
    var jqValidate = require('jqueryValidate');
    var jqForm = require('jqueryForm');
    var bootstrapJs = require('bootstrap');
    var bootstrap_tpl = require('js/common/bootstrap_tpl');

    var validateForm = $("#form1").validate({
        errorElement:'span',
        errorClass : 'help-block col-sm-2',
        debug:true,
        rules:{
            uname:{required:true,rangelength: [6,20]},
            password:{required:true,rangelength: [5,20]},
            password_confirm:{required:true,rangelength: [5,20],equalTo: "#password"},
            agree:{required:true}
        },
        messages:{
            uname:{
                required: "用户名不能为空",
                rangelength:'用户名必须为6-20位'
            },
            password:{
                required: "密码不能为空",
                rangelength:'密码长度必须在5-20位'
            },
            password_confirm: {
                required: "密码不能为空",
                rangelength:'密码长度必须在5-20位',
                equalTo: "两次密码输入不一致"
            },
            agree: {
                required: "用户协议必选"
            }
        },
        //自定义错误消息放到哪里
        errorPlacement : function(error, element) {
            myValidate.errorPlacement(error, element);
        },
        //给未通过验证的元素进行处理
        highlight : function(element) {
            myValidate.highlight(element);
        },
        //验证通过的处理
        success : function(label) {
            myValidate.success(label);
        }
    });

    var subCheck = {
        initialize:function(){
            this.initControl();
        },
        initControl:function(){
            var fn = this;
            $('#form1').submit(function(){
                $(this).ajaxSubmit({
                    type: "post",
                    beforeSubmit: $.proxy(validateForm.form, validateForm),
                    success:fn.subSuccess,
                    clearForm:false
                });
                return false;//此处必须返回false，阻止常规的form提交
            });
            $("#form1").keydown(function(e){
                if(e.keyCode == 13){
                    $("#form1").click();
                }
            });
        },
        subSuccess:function(result){
            //$("#regSub").attr('disabled',false);
            result = jQuery.parseJSON(result);
            if(result && result.status=='200'){
                $('.alert').remove();
                $("#form1").before( bootstrap_tpl.alertClass('注册成功', 1) );
                return false;
            }else{
                //console.log('登录失败',result.msg);
                var msg = '';
                $.each( result.msg , function(i, n){
                    msg += n + '<br/>';
                });
                $('.alert').remove();
                $("#form1").before(  bootstrap_tpl.alertClass(msg, 4)  );
                return false;
            }
        }
    }
    subCheck.initialize();

});